package info.jimao.newsdk;

import android.app.Application;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.OkHttpRequestBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.builder.PostStringBuilder;

public class ApiManager extends ApiConfig {
    protected Application context;
    private static ApiManager apiManager;


    public synchronized static ApiManager getInstance() {
        if (apiManager == null) {
            apiManager = new ApiManager();
        }
        return apiManager;
    }


   /*  * 初始化
     *
     *@param context   context
     *@param apiServer apiServer
     * */

    public void initialize(Application context, String apiServer) {
        this.context = context;
        this.apiServer = apiServer;
        // this.authUrl = authUrl;
        //initClientId();
        //this.deviceId = getDeviceId();
        // this.userAgent = generateUserAgent();
        //initAuthorization();
    }

    public PostFormBuilder post(String url) {
        PostFormBuilder builder = OkHttpUtils.post();
        fixUrl(builder, url);
        // addDefaultHeaders(builder);
        //addAuthHeaders(builder);
        return builder;
    }

    public PostStringBuilder postString(String url) {
        PostStringBuilder builder = OkHttpUtils.postString();
        fixUrl(builder, url);
        //addDefaultHeaders(builder);
        //addAuthHeaders(builder);
        return builder;
    }


    public GetBuilder get(String url) {
        GetBuilder builder = OkHttpUtils.get();
        fixUrl(builder, url);
        //addDefaultHeaders(builder);
        //addAuthHeaders(builder);
        return builder;
    }

    private void fixUrl(OkHttpRequestBuilder builder, String url) {
        builder.url(!isFullUrl(url) ? (apiServer + url) : url);
    }

   /* private void addDefaultHeaders(OkHttpRequestBuilder builder) {
        if (userAgent != null)
            builder.addHeader("User-Agent", userAgent);
        if (deviceId != null)
            builder.addHeader("device_id", deviceId);
    }

    private void addAuthHeaders(OkHttpRequestBuilder builder) {
        if (hasAuthorization()) {
            builder.addHeader("Authorization", "Bearer " + accessToken);
        }
    }*/

    private boolean isFullUrl(String url) {
        return url.startsWith("http://") || url.startsWith("https://");
    }

    /*protected boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnectedOrConnecting();
    }*/

   /* public boolean hasAuthorization() {
        return !TextUtils.isEmpty(accessToken);
    }

    public boolean hasIMAuthorization() {
        return !TextUtils.isEmpty(imAccessToken);
    }

    public String getIMToken(String cacheKey) {
        return (String) FileCacheUtils.getInstance(context).readDataCache(cacheKey);
    }

    public void setAuthorization(String accessToken, String refreshToken) {
        this.accessToken = accessToken;
        this.refreshToken = refreshToken;
        FileCacheUtils.getInstance(context).saveDataCache(accessToken, accessTokenKey);
        FileCacheUtils.getInstance(context).saveDataCache(refreshToken, refreshTokenKey);
    }

    *//*融云*//*
    public void setIMAuthorization(String imAccessToken) {
        this.imAccessToken = imAccessToken;
        FileCacheUtils.getInstance(context).saveDataCache(imAccessToken, imAccessTokenKey);
    }

    public void getToken(String userName, String password, TokenCallback callback) {
        post(authUrl)
                .addHeader("Authorization", "Basic")
                .addParams("grant_type", "password")
                .addParams("userName", userName)
                .addParams("password", password)
                .addParams("client_id", clientId)
                .addParams("client_secret", clientSecret)
                .tag(context)
                .build().execute(callback);
    }

    public void refreshToken(TokenCallback callback) {
        post(authUrl)
                .addHeader("Authorization", "Basic")
                .addParams("grant_type", "refresh_token")
                .addParams("refresh_token", refreshToken)
                .addParams("client_id", clientId)
                .addParams("client_secret", clientSecret)
                .tag(context)
                .build().execute(callback);
    }*/

    /*private void initAuthorization() {
        if (FileCacheUtils.getInstance(context).isExistDataCache(accessTokenKey)) {
            accessToken = getAccessToken(accessTokenKey);
        }

        if (FileCacheUtils.getInstance(context).isExistDataCache(refreshTokenKey)) {
            refreshToken = getRefreshToken(refreshTokenKey);
        }
        if (FileCacheUtils.getInstance(context).isExistDataCache(imAccessTokenKey)) {
            imAccessToken = getIMToken(imAccessTokenKey);
        }
    }*/

   /* private String getRefreshToken(String cacheKey) {
        return (String) FileCacheUtils.getInstance(context).readDataCache(cacheKey);
    }

    private String getAccessToken(String cacheKey) {
        return (String) FileCacheUtils.getInstance(context).readDataCache(cacheKey);
    }*/

   /* private void initClientId() {
        try {
            Bundle metaData = context.getPackageManager().getApplicationInfo(context
                    .getPackageName(), PackageManager.GET_META_DATA).metaData;
            this.clientId = metaData.getString("cn.hamazon.wuxiu.clientId");
            this.clientSecret = metaData.getString("cn.hamazon.wuxiu.clientSecret");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }*/

  /*  private String getDeviceId() {
        if (FileCacheUtils.getInstance(context).isExistDataCache(deviceIdKey)) {
            return (String) FileCacheUtils.getInstance(context).readDataCache(deviceIdKey);
        }

        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }*/

   /* public void initDeviceId() {
        deviceId = getDeviceId();
        FileCacheUtils.getInstance(context).saveDataCache(deviceId, deviceIdKey);
    }*/

/*    private String generateUserAgent() {
        return "WuXiu.Hamazon" + '/' + BuildConfig.VERSION_NAME + '_' +
                BuildConfig.VERSION_CODE +
                "/Android" +
                "/" + android.os.Build.VERSION.RELEASE +
                "/" + android.os.Build.MODEL;
    }*/

    private ApiManager() {
    }
}
